package com.joiant.community.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.joiant.community.dto.PaginationDTO;
import com.joiant.community.dto.QuestionDTO;
import com.joiant.community.mapper.QuestionMapper;
import com.joiant.community.mapper.UserMapper;
import com.joiant.community.model.Question;
import com.joiant.community.model.QuestionExample;
import com.joiant.community.model.User;
import com.joiant.community.model.UserExample;

@Service
public class QuestionService {
	
	@Autowired
	private QuestionMapper questionMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	public PaginationDTO list(Integer page, Integer size){
		
		PaginationDTO paginationDTO = new PaginationDTO();
		
		Integer totalPage = 0;
		
		Integer totalCount = (int)questionMapper.countByExample(new QuestionExample());
		
		if(totalCount % size == 0) {
			totalPage = totalCount /size;
		}else{
			totalPage = totalCount /size + 1;
		}
		if(page > totalPage) {
			page = totalPage;
		}
		if(page < 1) {
			page = 1;
		}
		
		
		paginationDTO.setPagination(totalPage, page);
		
		Integer offset = size * (page -1);
		List<Question> questions = questionMapper.selectByExampleWithRowbounds(new QuestionExample(), new RowBounds(offset, size));
		List<QuestionDTO> questionDTOList = new ArrayList<>();
		
		for(Question question: questions) {
			UserExample userExample = new UserExample();
			userExample.createCriteria().andIdEqualTo(question.getCreator());
			List<User> users = userMapper.selectByExample(userExample);
			if(users != null && users.size() > 0) {
				User user = users.get(0);
				QuestionDTO questionDTO = new QuestionDTO();
				BeanUtils.copyProperties(question, questionDTO);
				questionDTO.setUser(user);
				questionDTOList.add(questionDTO);
			}
			
		}
		paginationDTO.setQuestions(questionDTOList);
		
		
		
		return paginationDTO;
	}
	
	public PaginationDTO list(Integer userId,Integer page, Integer size){

		PaginationDTO paginationDTO = new PaginationDTO();
		
		Integer totalPage = 0;
		
		QuestionExample questionExample = new QuestionExample();
		questionExample.createCriteria().andCreatorEqualTo(userId);
		Integer totalCount = (int)questionMapper.countByExample(questionExample);
		
		if(totalCount % size == 0) {
			totalPage = totalCount /size;
		}else{
			totalPage = totalCount /size + 1;
		}
		
		if(page > totalPage) {
			page = totalPage;
		}
		if(page <= 1) {
			page = 1;
		}
		
		
		paginationDTO.setPagination(totalPage, page);

		Integer offset = size * (page -1);
		
		QuestionExample questionExample2 = new QuestionExample();
		questionExample2.createCriteria().andCreatorEqualTo(userId);
		List<Question> questions = questionMapper.selectByExampleWithRowbounds(questionExample2, new RowBounds(offset, size));
		List<QuestionDTO> questionDTOList = new ArrayList<>();

		for(Question question: questions) {
			UserExample userExample = new UserExample();
			userExample.createCriteria().andIdEqualTo(question.getCreator());
			List<User> users = userMapper.selectByExample(userExample);
			if(users != null && users.size() > 0) {
				User user = users.get(0);
				QuestionDTO questionDTO = new QuestionDTO();
				BeanUtils.copyProperties(question, questionDTO);
				questionDTO.setUser(user);
				questionDTOList.add(questionDTO);
			}
		}
		paginationDTO.setQuestions(questionDTOList);



		return paginationDTO;
	}

	public QuestionDTO getById(Integer id) {
		QuestionExample questionExample = new QuestionExample();
		questionExample.createCriteria().andIdEqualTo(id);
		List<Question> questions = questionMapper.selectByExample(questionExample);
		QuestionDTO questionDTO = new QuestionDTO();
		if(questions != null && questions.size() > 0) {
			Question question = questions.get(0);
			
			BeanUtils.copyProperties(question, questionDTO);
			
			
			UserExample userExample = new UserExample();
			userExample.createCriteria().andIdEqualTo(question.getCreator());
			List<User> users = userMapper.selectByExample(userExample);
			if(users != null && users.size() > 0) {
				User user = users.get(0);
				questionDTO.setUser(user);
			}
		}
		
		
		
		return questionDTO;
	}

	public void createOrUpdate(Question question) {
		if(question.getId() == null) {
			//创建
			question.setGmtCreater(System.currentTimeMillis());
			question.setGmtModified(question.getGmtCreater());
			questionMapper.insert(question);
		}else {
			//更新
			Question updateQuestion = new Question();
			updateQuestion.setTitle(question.getTitle());
			updateQuestion.setDescription(question.getDescription());
			updateQuestion.setTag(question.getTag());
			updateQuestion.setGmtModified(System.currentTimeMillis());
			
			QuestionExample questionExample = new QuestionExample();
			questionExample.createCriteria().andIdEqualTo(question.getId());
			questionMapper.updateByExampleSelective(updateQuestion, questionExample);
		}
	}

}
